package com.example.text0926;

import static com.example.text0926.R.*;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class File_MainActivity extends AppCompatActivity {

    Button button1,button2,button3,button4;
    TextView textView;
    SharedPreferences sp;
    FileOutputStream outputStream;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_file_main);

        button1=findViewById(R.id.button_file_1);
        button2=findViewById(R.id.button_file_2);
        button3=findViewById(R.id.button_file_3);
        button4=findViewById(R.id.button_file_4);

        textView=findViewById(R.id.textView_file_1);

        sp=getSharedPreferences("mml", Context.MODE_PRIVATE);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sp.edit().putString("my_name","Mai")
                        .putString("key","123456")
                        .commit();
                Log.d("mml","Data saved"+sp.getString("my_name","defailt_value"));

            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText(sp.getString("my_name","123"));
            }
        });

        File file = new File("data/data/com.example.text0926/mmlfile.txt");

        //写入文件
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                try {
                    outputStream = new FileOutputStream(file);
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }

                try {
                    outputStream.write("hello_world".getBytes(StandardCharsets.UTF_8));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }

        });

        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }
}